/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package fr.ove.openmath.jome.ctrlview.bidim;

/**
 *
 * @author Mr.Sam
 */
import java.awt.*;
import fr.ove.openmath.jome.ctrlview.bidim.Display;
import fr.ove.openmath.jome.ctrlview.bidim.DisplayLayout;
import fr.ove.openmath.jome.ctrlview.bidim.HorizontalLayout;
import fr.ove.openmath.jome.ctrlview.bidim.selection.events.SelectionEvent;
import fr.ove.openmath.jome.model.*;

/**
* A layout manager that lays components horyzontally, but the last one in the
* list, is treated as a underscript (there is an downward shitf).
*
* @author © 1999 DIRAT Laurent
* @version 2.0  13/12/1999
*/
public class UnderscriptLayout extends ScriptLayout {
    /**
    * Computes the size of the display according to its children size (if any),
    * and its different attributes.
    * @return the size of the display.
    */
    public Dimension computeAttributes() {
        int ascent = 0;
        int descent = 0;
        int width = 0;
        int height = 0;
        
        Display base, indice;
        int decalage = 0;
        
        base = (Display) displayToLay.getComponent(0);
        base.setSize(base.getPreferredSize());
        width += base.getWidth() + base.getShiftX();
        
        indice = (Display) displayToLay.getComponent(1);

        if (base.getLayout() instanceof UnderscriptLayout)
            ((DisplayLayout) indice.getLayout()).updateLevel(((Display) base.getComponent(1)).getLevel()+ 1);
        else
            ((DisplayLayout) indice.getLayout()).updateLevel(base.getLevel()+ 1);
        
        indice.setSize(indice.getPreferredSize());
        
        if (base.getLayout() instanceof UnderscriptLayout) {
            Display ind_base = (Display) base.getComponent(1);
            decalage = ind_base.getDescent() + ind_base.getShiftY();
        }
        else
            decalage = base.getDescent();

        /* ############################# */
        base.setShiftY(0); // ATTENTION : rajout !!!!!!
        /* ############################# */
        
        indice.setShiftY(decalage);
        width += indice.getWidth() + indice.getShiftX();
        
        displayToLay.setAscent(base.getAscent()); 
        displayToLay.setDescent(decalage + indice.getDescent());
        
        height += displayToLay.getAscent() + displayToLay.getDescent();
        displayToLay.setSize(width , height);
        
        displayToLay.setComputeAttributes(false);
        return new Dimension(width, height);
    }
}
